package com.example.xpvideo.service.serviceImpl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.xpvideo.context.PageContext;
import com.example.xpvideo.entity.BusinessDistrict;
import com.example.xpvideo.entity.Notice;
import com.example.xpvideo.entity.PageResult;
import com.example.xpvideo.entity.ResponseResult;
import com.example.xpvideo.entity.req.NoticeReq;
import com.example.xpvideo.entity.req.UpdateReq;
import com.example.xpvideo.enums.ErrCodeEnum;
import com.example.xpvideo.exception.ErrorException;
import com.example.xpvideo.mapper.NoticeMapper;
import com.example.xpvideo.service.NoticeService;
import com.example.xpvideo.service.TimeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.w3c.dom.Node;

import java.time.LocalDateTime;
import java.util.List;

/**
 * (Notice)Service层
 *
 * @author 杨丽辉
 * @since 2021-09-08 11:42:55
 */
@Service
 @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
public class NoticeServiceImpl implements NoticeService {
    @Autowired
    private NoticeMapper noticeMapper;
    @Autowired
    private TimeService timeService;

    @Override
    public ResponseResult<PageResult<List<Notice>>> list(NoticeReq noticeReq) throws ErrorException {

        int page = PageContext.getPage();
        int size = PageContext.getSize();
        String title = noticeReq.getTitle();
        Integer type = noticeReq.getType();
        PageHelper.startPage(page, size);

        List<Notice> list = noticeMapper.selectList(Wrappers.<Notice>query().eq("del",1)
                .eq(!ObjectUtils.isEmpty(title),"title",title)
                .eq(!ObjectUtils.isEmpty(type),"type",type));

        PageInfo pageInfo = new PageInfo<>(list);
        //当前页
        int pageNum = pageInfo.getPageNum();
        //总页数
        int pages = pageInfo.getPages();
        //当前页展示的数量
        int pageSize = pageInfo.getSize();
        //查询总数
        long count = pageInfo.getTotal();
        PageResult<List<Notice>> listPageResult = new PageResult<>(count, pages, pageSize, pageNum, list);

        return ResponseResult.responseResult(0, listPageResult);

    }

    @Override
    public ResponseResult<Object> insert(Notice notice)throws ErrorException{
        String title = notice.getTitle();
        String content = notice.getContent();
        Integer type = notice.getType();

        Notice notice1 = new Notice();
        notice1.setTitle(title);
        notice1.setContent(content);
        notice1.setType(type);
        notice1.setCreateTime(timeService.getTime());
        notice1.setDel(1);
        int res = noticeMapper.insert(notice1);

        if(res > 0){
            return ResponseResult.responseResult(0,"发布公告成功");
        }
        throw new ErrorException(ErrCodeEnum.unknown_20002);
    }

    @Override
    public ResponseResult<Object> update(Notice notice)throws ErrorException{
        Integer id = notice.getId();
        String title = notice.getTitle();
        String content = notice.getContent();


        Notice notice1 = new Notice();
        notice1.setId(id);
        notice1.setTitle(title);
        notice1.setContent(content);
        notice1.setUpdateTime(timeService.getTime());
        notice1.setDel(1);
        int res = noticeMapper.updateNotice(notice1);

        if(res > 0){
            return ResponseResult.responseResult(0,"修改公告成功");
        }
        throw new ErrorException(ErrCodeEnum.unknown_20002);
    }

    @Override
    public ResponseResult<Object> delete(UpdateReq updateReq)throws ErrorException{
        Integer id = updateReq.getId();
        Integer stage = updateReq.getStage();


        Notice notice = new Notice();
        notice.setId(id);
        notice.setDel(stage);
        notice.setUpdateTime(timeService.getTime());
        int res = noticeMapper.updateNotice(notice);

        if(res > 0){
            return ResponseResult.responseResult(0,"删除公告成功");
        }
        throw new ErrorException(ErrCodeEnum.unknown_20002);
    }
}

